<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="/WEB-INF/jsp/includeCss.jsp"%>
<%@ include file="/WEB-INF/jsp/includeJs.jsp"%>
<%@ include file="/WEB-INF/jsp/includeTaglib.jsp"%>
<script type="text/javascript" src="<c:url value='/js/ztree.province.city.js' />"></script>
<style type="text/css">
  #selectable .ui-selecting { background: #4169E1; }
  #selectable .ui-selected { background: #6495ED; color: white; }
  #selectable { list-style-type: none; margin: 0; padding: 0; width: 95%; }
  #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
  /*.hidden_check {}*/
  .hidden_check {display: none;}
</style>

<table width="100%" border="0" cellpadding="0" cellspacing="5">
  <tr>
    <td valign="top">
      <table class="dbborder" width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="bottom" class="blueTableBg">&nbsp;配置管理&nbsp;-&nbsp;角色权限维护</td>
        </tr>
        <tr>
          <td>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td>
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td class="dashTopLeft"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td class="dashTop" width=100%><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td class="dashTopRight"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashLeft"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td align="center">
                        <form target="tempFrame" id="editPermissionRole" method="post" action="editPermissionRole.html" >
                        <table width="100%" border="0" cellpadding="0" cellspacing="3" class="globalText">
                          <tr style="vertical-align: top;">
                            <td class="globalText">
                              <div>
                                <dl id="selectable">
                                  <c:forEach var="role" items="${roles}" varStatus="status">
                                    <dd>
                                      <div style="padding: 1px;" class="group">
                                        <%--optimize--%>
                                        <label class="group_item" style="cursor: pointer; border: 0; color: #4B64EF" id="roleShowId_${role.id}"
                                               onclick="$('.group .group_item').each(function(index, elem) {
                                                          $(elem).css('color','#4b64ef');
                                                        });
                                                        $(this).css('color','#ff0000');
                                                        var en = [], divis = [], orgs = [];
                                                        <c:forEach var="en" items="${role.permissionEntries}">en.push('${en}');</c:forEach>
                                                        <c:forEach var="en" items="${role.managedAdminDivisionCodeEntries}">divis.push(${en});</c:forEach>
                                                        <c:forEach var="en" items="${role.managedOrgCodeEntries}">orgs.push('${en}');</c:forEach>
                                                        showRoleDetail(en, divis, orgs, '${role.id}', ${role.advanced}, ${role.useADFilter});
                                                        loadDivisionTree(divis);
                                                        updateUI();
                                                   ">${role.name}</label>
                                      </div>
                                    </dd>
                                  </c:forEach>
                                </dl>
                              </div>
                              <input type="button" class="btnStyle" value="保存" onclick="doSubmit()" style="margin-top: 10px">
                            </td>
                            <td>
                              <div class="mockTree">
                                <dl>
                                  <dt>
                                    <input class="entries" id="chk_myPlanGroup" value="${MODULE_MYPLAN_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_myPlan')" name="permissionEntries" type="checkbox">
                                    <label for="chk_myPlanGroup">我的计划任务权限</label>
                                    <input class="entries hidden_check" id="chk_myPlan" value="${MODULE_MYPLAN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                  </dt>
                                  <dd>
                                    <input class="entries" id="chk_myPlanCoStoreVisiting" value="${MODULE_MYPLAN_CO_STOREVISITING_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_CO_STOREVISITING_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="noneBothCheck(this, 'chk_subPlanRequireAudit')" name="permissionEntries" type="checkbox">
                                    <label for="chk_myPlanCoStoreVisiting">指定协同巡店</label>
                                  </dd>
                                  <dt>
                                    <input class="entries" id="chk_subGroup" value="${MODULE_SUB_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_sub')" name="permissionEntries" type="checkbox">
                                    <label for="chk_subGroup">下属工作跟踪权限</label>
                                    <input class="entries hidden_check" id="chk_sub" type="checkbox" name="permissionEntries" value="${MODULE_SUB_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}" />
                                  </dt>
                                  <dd>
                                    <input class="entries" id="chk_subTaskAssign" value="${MODULE_SUB_TASK_ASSIGN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_TASK_ASSIGN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_subTaskAssign">下属任务指派</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_subPlanTaskView" value="${MODULE_SUB_PLANTASK_VIEW_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLANTASK_VIEW_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="postConditionUncheck(this, 'chk_subPlanTaskAudit')" name="permissionEntries" type="checkbox">
                                    <label for="chk_subPlanTaskView">下属计划任务查看</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_subPlanTaskAudit" value="${MODULE_SUB_PLANTASK_AUDIT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLANTASK_AUDIT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="preConditionCheck(this, 'chk_subPlanTaskView')" name="permissionEntries" type="checkbox">
                                    <label for="chk_subPlanTaskAudit">下属计划任务审核</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_subPlanRequireAudit" value="${MODULE_SUB_PLAN_NEEDTOBE_AUDITED_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLAN_NEEDTOBE_AUDITED_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="noneBothCheck(this, 'chk_myPlanCoStoreVisiting')" name="permissionEntries" type="checkbox">
                                    <label for="chk_subPlanRequireAudit">下属计划需上级审核</label>
                                  </dd>
                                  <dt>
                                    <input class="entries" id="chk_projectGroup" value="${MODULE_PROJMGMT_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_PROJMGMT_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_project')" name="permissionEntries" type="checkbox">
                                    <label for="chk_projectGroup">项目管理权限</label>
                                    <input class="entries hidden_check" id="chk_project" value="${MODULE_PROJMGMT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_PROJMGMT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                  </dt>
                                  <dt>
                                    <input class="entries" id="chk_messageGroup" value="${MODULE_MSG_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MSG_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_message')" name="permissionEntries" type="checkbox">
                                    <label for="chk_messageGroup">消息公告权限</label>
                                    <input class="entries hidden_check" id="chk_message" value="${MODULE_MSG_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MSG_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                  </dt>
                                  <dt>
                                    <input class="entries" id="chk_adminGroup" value="${MODULE_ADMIN_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_admin')" name="permissionEntries" type="checkbox">
                                    <label for="chk_adminGroup">配置项权限</label>
                                    <input class="entries hidden_check" id="chk_admin" value="${MODULE_ADMIN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                  </dt>
                                  <dd>
                                    <input class="entries" id="chk_adminUserACL" value="${MODULE_ADMIN_USR_ACL_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_USR_ACL_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_adminuseracl">用户权限管理</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_adminWorkflow" value="${MODULE_ADMIN_WORKFLOW_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_WORKFLOW_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_adminWorkflow">工作流程配置</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_adminProject" value="${MODULE_ADMIN_PROJECT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_PROJECT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_adminProject">项目管理配置</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_adminStore" value="${MODULE_ADMIN_STORE_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_STORE_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_adminStore">门店管理配置</label>
                                  </dd>
                                  <dd>
                                    <input class="entries" id="chk_adminRoleTask" value="${MODULE_ADMIN_ROLETASK_AIM_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_ROLETASK_AIM_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                    <label for="chk_adminRoleTask">员工任务目标配置</label>
                                  </dd>
                                  <dt>
                                    <input class="entries" id="chk_reportGroup" value="${MODULE_RPT_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_RPT_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           onchange="syncCheck(this, 'chk_report')" name="permissionEntries" type="checkbox">
                                    <label for="chk_reportGroup">分析报表权限</label>
                                    <input class="entries hidden_check" id="chk_report" value="${MODULE_RPT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_RPT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                           name="permissionEntries" type="checkbox">
                                  </dt>
                                  <dd>
                                    <input type="checkbox" id="useADFilter" name="useADFilter" value="1"
                                           onchange="postConditionUncheck(this, 'isAdvanced')">
                                    <label for="useADFilter">报表区域限制 ==&gt;</label>
                                  </dd>
                                  <dt>
                                    <input type="checkbox" id="isAdvanced" name="isAdvanced" value="1"
                                           onchange="preConditionCheck(this, 'chk_reportGroup'); preConditionCheck(this, 'useADFilter')">
                                    <label for="isAdvanced">高级管理者 ==&gt;</label>
                                  </dt>
                                </dl>
                              </div>
                            </td>
                            <td>
                              <span class="mockTree" id="useADFilterPane" style="width: 200px;">
                                <label>报表区域限制</label>
                                <span id="adTree" class="ztree"></span>
                              </span>
                            </td>
                            <td>
                              <span class="mockTree" id="isAdvancedPane">
                                <label>高级管理者项目查阅权限</label>
                                <span id="orgTree" class="ztree"></span>
                              </span>
                            </td>
                          </tr>
                          <%--<tr>--%>
                            <%--<td><input type="button" class="btnStyle" value="保存" onclick="doSubmit()"></td>--%>
                          <%--</tr>--%>
                          <input type="hidden" name="updatekey" id="roleId" />
                          <input type="hidden" name="name" id="roleName" />
                          <input type="hidden" name="managedAdminDivisionCodeEntries" id="divisionCodes" />
                          <input type="hidden" name="managedOrgCodeEntries" id="orgNodes" />
                        </table>
                        </form>
                      </td>
                      <td class="dashRight"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<script type="text/javascript">
  function noneBothCheck(src, dst_id) {
    $(src).prop('checked') && $('#' + dst_id).prop('checked', false);
    updateUI();
  }

  function preConditionCheck(src, dst_id) {
    $(src).prop('checked') && $('#' + dst_id).prop('checked', true);
    updateUI();
  }

  function postConditionUncheck(src, dst_id) {
    $(src).prop('checked') || $('#' + dst_id).prop('checked', false);
    updateUI();
  }

  function syncCheck(src, dst_id) {
    $('#' + dst_id).prop('checked', $(src).prop('checked'));
    updateUI();
  }

  function updateUI() {
    //我的计划任务
    if ($('#chk_myPlanGroup').prop('checked')) {
      $('#chk_myPlanCoStoreVisiting').removeAttr('disabled');
    } else {
      $('#chk_myPlanCoStoreVisiting').prop('checked', false).attr('disabled', 'disabled');
    }
    //下属工作跟踪
    if ($('#chk_subGroup').prop('checked')) {
      $('#chk_subTaskAssign').removeAttr('disabled');
      $('#chk_subPlanTaskView').removeAttr('disabled');
      $('#chk_subPlanTaskAudit').removeAttr('disabled');
      $('#chk_subPlanRequireAudit').removeAttr('disabled');
    } else {
      $('#chk_subTaskAssign').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanTaskView').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanTaskAudit').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanRequireAudit').prop('checked', false).attr('disabled', 'disabled');
    }
    //配置管理
    if ($('#chk_adminGroup').prop('checked')) {
      $('#chk_adminUserACL').removeAttr('disabled');
      $('#chk_adminWorkflow').removeAttr('disabled');
      $('#chk_adminProject').removeAttr('disabled');
      $('#chk_adminStore').removeAttr('disabled');
      $('#chk_adminRoleTask').removeAttr('disabled');
    } else {
      $('#chk_adminUserACL').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminWorkflow').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminProject').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminStore').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminRoleTask').prop('checked', false).attr('disabled', 'disabled');
    }
    //报表
    if ($('#chk_reportGroup').prop('checked')) {
      $('#useADFilter').removeAttr('disabled');
    } else {
      $('#useADFilter').prop('checked', false).attr('disabled', 'disabled');
      $('#isAdvanced').prop('checked', false);
    }
    //高级管理者
    if ($('#isAdvanced').prop('checked')) {
      $('#chk_reportGroup').removeAttr('disabled').prop('checked', true);
      $('#chk_report').prop('checked', true);
      $('#useADFilter').removeAttr('disabled').prop('checked', true);
    }
  }

  function doSubmit(){
    var divisionCodes = '';
    if ($('#useADFilter').prop('checked')) {
      var divisionNode = $.fn.zTree.getZTreeObj("adTree").getCheckedNodes(true);
      divisionNode.forEach(function(e) {
        e.getCheckStatus().half || (divisionCodes += e.code + ',')
      });
      divisionCodes.length > 0 && (divisionCodes = divisionCodes.slice(0, -1))
    }
    var orgNodes = '';
    if ($('#isAdvanced').prop('checked')) {
      var orgNode = $.fn.zTree.getZTreeObj("orgTree").getCheckedNodes(true);
      orgNode.forEach(function(e) {
        e.getCheckStatus().half || (orgNodes += e.id + ',')
      });
      orgNodes.length > 0 && (orgNodes = orgNodes.slice(0, -1))
    }
    $('#divisionCodes').val(divisionCodes);
    $('#orgNodes').val(orgNodes);
    $('#roleName').val($('#roleShowId_' + $('#roleId').val()).text());
    $('#editPermissionRole').submit();
  }

  function showRoleDetail(values, divis, orgs, roleId, isAdvanced, useADFilter){
    $('#isAdvanced').prop('checked', !!isAdvanced);
    $('#useADFilter').prop('checked', !!useADFilter);
    $('.entries').each(function() {
      $(this).prop("checked", false);
    });
    values.forEach(function(e) {
      $('input[value="' + e + '"]').prop('checked', true)
    });
    var tree = $.fn.zTree.getZTreeObj('orgTree');
    tree.checkAllNodes(false);
    var node;
    orgs.forEach(function(e) {
      node = tree.getNodeByParam("id", e);
      tree.checkNode(node, true, true);
    });
    $('#roleId').val(roleId);
  }

  function loadDivisionTree(divis) {
    var tree = $.fn.zTree.getZTreeObj('adTree');
    var root = tree.getNodes()[0];
    tree.checkNode(root, false, true);
    var node;
    divis.forEach(function(e) {
      node = tree.getNodeByParam('code', e);
      node && tree.checkNode(node, true, true);
    });
  }

  function submitCallbackSuccess(obj) {
    window.location.reload();
  }

  (function() {
    $.getJSON("<c:url value='/json/findAllOrgnizations.html' />", function(zNodes) {
      $.fn.zTree.init($("#orgTree"), {check: {enable: true}, data: {simpleData: {enable: true}}}, zNodes).expandAll(true);
    });

    var tree = $.fn.zTree.init($('#adTree'), {check: {enable: true}}, ztree_province_city);
    tree.expandNode(tree.getNodes()[0], true);

    updateUI();
  })();
</script>
